<?php
/**
 * This class represents 'medicine_coefficient' table.
 *
 * @package dept_medicine.core.ordo 
 * @author pktquynh
 * @version $Id:$
 * @copyright 2009 LHV Company
 */
class MedicineCoefficient extends ORDataObject {
    var $medicine_coefficient_id = '';
    var $department_id = '';
    var $medicine_id = '';
    var $quantity_num = '';
    var $quantity_title = '';
	
    var $_table         = 'medicine_coefficient';
    var $_internalName  = 'MedicineCoefficient';
    var $_key           = 'medicine_coefficient_id';
    
	public function MedicineCoefficient($id = 0) {
        //call the parent constructor so we have a _db to work with
        parent::ORDataObject();     
    }
    
    /**
     * Get all medicine coefficient by department and medicine type.
     * @param $deptId The id of department
     * @param $medTypeId The id of medicine type
     * @return array of medicine coefficient
     */
    public function getMedicineCoefs($deptId, $medTypeId) {
        $fieldName =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
        $fieldQuantityTitle =   L10nHelper::changeField(L10nHelper::getLocale(), 'quantity_title');
        $ds = new Datasource_sql();
        $where = '';
        if ($deptId != 'all') {
            $where = " d.department_id = '" . $deptId . "'";
        }
        if ($medTypeId != 'all') {
        	if ($where == '') {
        		$where = " et.equipment_type_id = '" . $medTypeId . "'";
        	} else {
        		$where = $where . " AND et.equipment_type_id = '" . $medTypeId . "'";
        	}
        }
        $sql = array (
            'cols'    => "mc.medicine_coefficient_id as med_coef_id, d.$fieldName as department_name, 
                        et.$fieldName as medicine_type, m.name as medicine_name,"
                         . "mc.quantity_num as quantity_num, mdu.$fieldQuantityTitle as quantity_title, mc.deleted",
            'from'    => $this->_table . " mc"
                         . " LEFT JOIN department d ON mc.department_id = d.department_id "
                         . " LEFT JOIN medicine m ON mc.medicine_id = m.medicine_id "
                         . " LEFT JOIN equipment_type et ON m.functional_type = et.equipment_type_id"
                         . " LEFT JOIN medicine_default_usage mdu ON m.default_usage_id = mdu.id",                        
            'where'   => $where, 
            'orderby' => "department_name, medicine_type, medicine_name");
                         
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();        
        return $result;
    }

    /**
     * Get information of a medicine coefficient (department, medicine type, medicine name ...)
     * @param $medCoefId The id of medicine coefficient
     * @return array contains information of the medicine coefficient
     */
    public function getMedicineCoef($medCoefId) {
    	$field =   L10nHelper::changeField(L10nHelper::getLocale(), 'quantity_title');
        $ds = new Datasource_sql();
        $sql = array (
            'cols'    => "mc.medicine_coefficient_id as med_coef_id, mc.department_id , m.functional_type as medicine_type_id, "
                         . "mc.medicine_id, mc.quantity_num, mdu.$field as quantity_title, mc.deleted",
            'from'    => $this->_table . " mc"
                         . " LEFT JOIN medicine m ON mc.medicine_id = m.medicine_id "
                         . " LEFT JOIN medicine_default_usage mdu ON m.default_usage_id = mdu.id",                         
            'where'   => " mc.medicine_coefficient_id = '" . $medCoefId . "'");
                         
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
            return false;
        } else {
            return $result[0];
        }        
    }
    
    /**
     * Check whether or not medicine coefficient has been existed for some department and medicine.
     * @param $deptId The id of department
     * @param $medId The id of medicine
     * @param $medCoefId The id of medicine coefficient (in case of editing the old medicine coef.)
     * @return boolean true if medicine coef. existed, otherwise return false
     */
    public function checkMedicineCoefExisted($deptId, $medId, $medCoefId) {
    	$ds = new Datasource_sql();
    	$medCoefIdCond = '';
    	if (isset($medCoefId) && $medCoefId != "") {
    		$medCoefIdCond = " AND medicine_coefficient_id != '" . $medCoefId . "'";
    	}
        $sql = array (
            'cols'    => "medicine_coefficient_id",
            'from'    => $this->_table,
            'where'   => "department_id = '" . $deptId . "' AND medicine_id = '" . $medCoefId . "'"
                       . $medCoefIdCond);
                         
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
        	return false;
        } else {
        	return true;
        }
    }
    
    /**
     * Modify 'deleted' field of a medicine coefficient by the corresponding action.
     * @param $medCoefId The id of medicine coefficient
     * @param $action indicates remove or undo action
     */
    public function removeOrUndoMedicineCoef($medCoefId, $action) {
    	$sql = '';
        if ($action == 'remove') {
            $sql = "UPDATE medicine_coefficient SET deleted = 1 WHERE medicine_coefficient_id = '$medCoefId'";
        } else if ($action == 'undo') {
            $sql = "UPDATE medicine_coefficient SET deleted = 0 WHERE medicine_coefficient_id = '$medCoefId'";
        }
        $this->_execute($sql);
    }
}
?>